<?php


namespace Decorator;


class Client
{
    public static function main()
    {
        // 创建原始构件
        $component = new ComponentA();

        // 倾斜+删除 装饰
        $decorator = new DecoratorA($component);
        // 加粗+超链接 装饰
        $decorator = new DecoratorB($decorator);

        // 取得装饰后的结果
        echo $decorator->operation(), PHP_EOL;
    }
}


// 被装饰构件
interface Component
{
    public function operation(): string;
}
class ComponentA implements Component
{
    public function operation(): string
    {
        return 'operation A from ComponentA.';
    }
}
class ComponentB implements Component
{
    public function operation(): string
    {
        return 'operation B from ComponentB.';
    }
}


// 装饰器
abstract class Decorator implements Component
{
    protected Component $component;

    /**
     * @param Component $component
     */
    public function __construct(Component $component)
    {
        $this->component = $component;
    }

    /**
     * @param Component $component
     */
    public function setComponent(Component $component): void
    {
        $this->component = $component;
    }

    abstract public function operation(): string;
}
class DecoratorA extends Decorator
{

    public function operation(): string
    {
        // 获取原始或前一次装饰后的结果
        $result = $this->component->operation();

        // 进行[再]装饰
        $result = "<i>$result</i>";  // 字体倾斜
        $result = "<s>$result</s>";  // 字体删除
        return $result;
    }
}
class DecoratorB extends Decorator
{

    public function operation(): string
    {
        // 获取原始或前一次装饰后的结果
        $result = $this->component->operation();

        // 进行[再]装饰
        $result = "<b>$result</b>";  // 字体加粗
        $result = "<a>$result</a>";  // 超链接
        return $result;
    }
}


Client::main();
